# Windows防火墙配置脚本
# 请以管理员权限运行PowerShell

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  MQTT项目 - Windows防火墙配置" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Write-Host "[错误] 请以管理员权限运行此脚本" -ForegroundColor Red
    Write-Host "右键点击PowerShell，选择'以管理员身份运行'" -ForegroundColor Yellow
    Write-Host ""
    Read-Host "按Enter键退出"
    exit 1
}

Write-Host "[√] 管理员权限确认" -ForegroundColor Green
Write-Host ""

# 配置端口
$ports = @(
    @{Port=3002; Name="MQTT-HTTP"; Description="MQTT Web应用HTTP服务"},
    @{Port=8081; Name="MQTT-WebSocket"; Description="MQTT WebSocket服务"},
    @{Port=3307; Name="MQTT-MySQL"; Description="MySQL数据库服务"}
)

Write-Host "准备配置以下防火墙规则：" -ForegroundColor Cyan
foreach ($portConfig in $ports) {
    Write-Host "  - 端口 $($portConfig.Port): $($portConfig.Description)" -ForegroundColor White
}
Write-Host ""

$confirm = Read-Host "是否继续? (Y/N)"
if ($confirm -ne 'Y' -and $confirm -ne 'y') {
    Write-Host "已取消配置" -ForegroundColor Yellow
    exit 0
}

Write-Host ""
Write-Host "开始配置防火墙规则..." -ForegroundColor Cyan
Write-Host ""

$successCount = 0
$failCount = 0

foreach ($portConfig in $ports) {
    $port = $portConfig.Port
    $name = $portConfig.Name
    $description = $portConfig.Description

    Write-Host "配置端口 $port ($name)..." -ForegroundColor White

    try {
        # 检查规则是否已存在
        $existingRule = Get-NetFirewallRule -DisplayName $name -ErrorAction SilentlyContinue

        if ($existingRule) {
            Write-Host "  [!] 规则已存在，删除旧规则..." -ForegroundColor Yellow
            Remove-NetFirewallRule -DisplayName $name
        }

        # 创建新规则
        New-NetFirewallRule `
            -DisplayName $name `
            -Description $description `
            -Direction Inbound `
            -LocalPort $port `
            -Protocol TCP `
            -Action Allow `
            -Enabled True `
            -Profile Any | Out-Null

        Write-Host "  [√] 端口 $port 配置成功" -ForegroundColor Green
        $successCount++
    }
    catch {
        Write-Host "  [×] 端口 $port 配置失败: $($_.Exception.Message)" -ForegroundColor Red
        $failCount++
    }
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  配置完成" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "成功: $successCount 项" -ForegroundColor Green
Write-Host "失败: $failCount 项" -ForegroundColor $(if($failCount -gt 0){"Red"}else{"Green"})
Write-Host ""

if ($successCount -gt 0) {
    Write-Host "已配置的防火墙规则：" -ForegroundColor Cyan
    Get-NetFirewallRule | Where-Object {$_.DisplayName -like "MQTT-*"} | Format-Table DisplayName, Enabled, Direction, Action -AutoSize
    Write-Host ""
}

Write-Host "提示：" -ForegroundColor Yellow
Write-Host "1. 防火墙规则已配置，但还需要在阿里云控制台配置安全组" -ForegroundColor White
Write-Host "2. 访问地址: http://8.149.243.80:3002" -ForegroundColor White
Write-Host "3. 如需移除规则，请运行: Remove-NetFirewallRule -DisplayName 'MQTT-*'" -ForegroundColor White
Write-Host ""

Read-Host "按Enter键退出"
